(function($) {
	var list = {};
	$.oEditor = function(id, config) {
	            if(!id || !list[id]) {
	                var D = new OEditor(config);
	                id = !id ? D.elem[0].id : id;
	                list[id] = D;
	            }
	            return list[id];
	        };

	$.oFEditor = function(id) {
	            if(list[id]) {
		            return list[id];
	            }
	            return null;
	        };
	
	
	var _id_counter = 0;
	
	function OEditor(config) {
		var self = this, cfg = self.config;
		cfg = mix(
				{
					id : null,
					className : 'editor',
					remote : null,
					initialtext : '',
					editorcurrentheight : 130, 
					editorminheight : 130,
					editorcontroltop:false,
					needfocus:false,
					editorid : 'editor'
				}, config || {});
		cfg.id = cfg.id || 'ui-editor-' + counter();
		self.config = cfg;
		self._createHTML();
	}

	mix(
			OEditor.prototype,{
				writeEditorContents:function(text){
					var self = this, cfg = self.config;
					if(self.wysiwyg) {
						if(text == '' && (BROWSER.firefox || BROWSER.opera)) {
							text = '<p></p>';
						}
						//text = bbcode2html(text);
						if(self.initialized && !(BROWSER.firefox && BROWSER.firefox >= '3' || BROWSER.opera)) {
							self.editdoc.body.innerHTML = text;
						} else {
							text = '<!DOCTYPE html PUBLIC "-/' + '/W3C/' + '/DTD XHTML 1.0 Transitional/' + '/EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' +
								'<html><head id="editorheader"><meta http-equiv="Content-Type" content="text/html; charset='+charset+'" />' +
								(BROWSER.ie && BROWSER.ie > 7 ? '<meta http-equiv="X-UA-Compatible" content="IE=7" />' : '' ) +
								'<link rel="stylesheet" type="text/css" href="'+base_url+'themes/puzzing/editor/wysiwyg.css" />' +
								(BROWSER.ie ? '<script>window.onerror = function() { return true; }</script>' : '') +
								'</head><body>' + text + '</body></html>';
							self.editdoc.designMode = 'off';
							self.editdoc = self.editwin.document;
							self.editdoc.open('text/html', 'replace');
							self.editdoc.write(text);
							self.editdoc.close();
							if(!BROWSER.ie) {
								var scriptNode = document.createElement("script");
								scriptNode.type = "text/javascript";
								scriptNode.text = 'window.onerror = function() { return true; }';
								self.editdoc.getElementById('editorheader').appendChild(scriptNode);
							}
							self.editdoc.body.contentEditable = true;
							self.editdoc.body.spellcheck = false;
							self.initialized = true;
							if(BROWSER.safari) {
								self.editdoc.onclick = self.safariSel;
							}
						}
					} else {
						self.textobj.value = text;
					}
					self.setEditorStyle();
				},
				safariSel:function(e){
					var self = this, cfg = self.config;
					e = e.target;
					if(e.tagName.match(/(img|embed)/i)) {
						var sel = self.editwin.getSelection(),rng= self.editdoc.createRange(true);
						rng.selectNode(e);
						sel.removeAllRanges();
						sel.addRange(rng);
					}
				},
				setEditorStyle:function(){
					var self = this, cfg = self.config, editorid = self.config.editorid;
					if(self.wysiwyg) {
						self.textobj.style.display = 'none';
						self.editbox.style.display = '';
						self.editbox.className = self.textobj.className;
						if(BROWSER.ie) {
							self.editdoc.body.style.border = '0px';
							self.editdoc.body.addBehavior('#default#userData');
							self.editwin.focus();
						}
						//self.editbox.style.height =self.editbox.contentWindow.document.body.style.height = self.config.editorcurrentheight + 'px';
						if(PZEM(editorid + '_iframe')) {
							PZEM(editorid + '_iframe').style.height = PZEM(editorid + '_iframe').contentWindow.document.body.style.height = self.config.editorcurrentheight + 'px';
						}
					} else {
						var iframe = self.textobj.parentNode.getElementsByTagName('iframe')[0];
						if(iframe) {
							self.textobj.style.display = '';
							self.textobj.style.height = self.config.editorcurrentheight + 'px';
							iframe.style.display = 'none';
						}
					}
				},
				addSnapshot:function(str){
					var self = this, cfg = self.config;
					if(self.stack[self.cursor] == str) {
						return;
					} else {
						self.cursor++;
						self.stack[self.cursor] = str;

						if(!isUndefined(self.stack[self.cursor + 1])) {
							self.stack[self.cursor + 1] = null;
						}
					}
				},
				setCaretAtEnd:function() {
					var self = this, cfg = self.config, wysiwyg=self.wysiwyg;
					if(wysiwyg) {
						self.editdoc.body.innerHTML += '';
					} else {
						self.editdoc.value += '';
					}
				},
				moveCursor:function(increment) {
					var self = this, cfg = self.config;
					var test = self.cursor + increment;
					if(test >= 0 && self.stack[test] != null && !isUndefined(self.stack[test])) {
						self.cursor += increment;
					}
				},
				getSnapshot:function () {
					var self = this, cfg = self.config;
					if(!isUndefined(self.stack[self.cursor]) && self.stack[self.cursor] != null) {
						return self.stack[self.cursor];
					} else {
						return false;
					}
				},
				checklength:function() {
					var self = this, cfg = self.config, editorid=cfg.editorid;
					var message = self.wysiwyg ? html2bbcode(self.getEditorContents()) : parseurl(self.textobj.value);
					PZEM(editorid + '_chck_tip').innerHTML = '&nbsp;还可以输入' + (self.postmaxchars-mb_strlen(message))+'字符（'+self.postminchars+'-'+self.postmaxchars+'）';
				},
				setEditorEvents:function() {
					var self = this, cfg = self.config;
					if(BROWSER.firefox || BROWSER.opera) {
						self.editdoc.addEventListener('mouseup', function(e) {self.mouseUp(e);}, true);
						self.editdoc.addEventListener('keyup', function(e) {self.keyUp(e);}, true);
						self.editwin.addEventListener('keydown', function(e) {self.keyDown(e);}, true);
					} else if(self.editdoc.attachEvent) {
						try{
							self.editdoc.body.attachEvent('onmouseup', function(e){ return self.mouseUp(e);});
							self.editdoc.body.attachEvent('onkeyup', function(e){ return self.keyUp(e);});
							self.editdoc.body.attachEvent('onkeydown', function(e){ return self.keyDown(e);});
						} catch(e) {}
					}
				},
				mouseUp:function(event) {
					var self = this, cfg = self.config;
					if(self.wysiwyg) {
						self.setContext();
					}
				},
				keyUp:function(event) {
					var self = this, cfg = self.config;
					if(self.wysiwyg) {
						self.setContext();
					}
					self.checklength();
				},
				keyDown:function(event) {
					var self = this, cfg = self.config;
					self.ctlent(event);
				},
				setContext:function(cmd) {
					var self = this, cfg = self.config;
					var cmd = !cmd ? '' : cmd;
					var contextcontrols = new Array('bold', 'italic', 'underline', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist');
					for(var i in contextcontrols) {
						var controlid = contextcontrols[i];
						var obj = PZEM(cfg.editorid + '_' + controlid);
						if(obj != null) {
							if(cmd == 'clear') {
								obj.className = '';
								continue;
							}
							try {
								var state = self.editdoc.queryCommandState(contextcontrols[i]);
							} catch(e) {
								var state = false;
							}
							if(isUndefined(obj.state)) {
								obj.state = false;
							}
							if(obj.state != state) {
								obj.state = state;
								self.buttonContext(obj, state ? 'mouseover' : 'mouseout');
							}
						}
					}

					try {
						var fs = self.editdoc.queryCommandValue('fontname');
					} catch(e) {
						fs = null;
					}
					if(fs == '' && !BROWSER.ie && window.getComputedStyle) {
						fs = self.editdoc.body.style.fontFamily;
					} else if(fs == null) {
						fs = '';
					}
					fs = fs && cmd != 'clear' ? fs : '字体';
					if(fs != PZEM(cfg.editorid + '_font').fontstate) {
						thingy = fs.indexOf(',') > 0 ? fs.substr(0, fs.indexOf(',')) : fs;
						PZEM(cfg.editorid + '_font').innerHTML = thingy;
						PZEM(cfg.editorid + '_font').fontstate = fs;
					}
					try {
						var ss = self.editdoc.queryCommandValue('fontsize');
						if(ss == null || ss == '' || cmd == 'clear') {
							ss = self.formatFontsize(self.editdoc.body.style.fontSize);
						} else {
							var ssu = ss.substr(-2);
							if(ssu == 'px' || ssu == 'pt') {
								ss = self.formatFontsize(ss);
							}
						}
					} catch(e) {
						ss = '大小';
					}

					if(ss != PZEM(cfg.editorid + '_size').sizestate) {
						if(PZEM(cfg.editorid + '_size').sizestate == null) {
							PZEM(cfg.editorid + '_size').sizestate = '';
						}
						PZEM(cfg.editorid + '_size').innerHTML = ss;
						PZEM(cfg.editorid + '_size').sizestate = ss;
					}
				},
				buttonContext:function (obj, state) {
					if(state == 'mouseover') {
						obj.style.cursor = 'pointer';
						var mode = obj.state ? 'down' : 'hover';
						if(obj.mode != mode) {
							obj.mode = mode;
							obj.className = 'hover';
						}
					} else {
						var mode = obj.state ? 'selected' : 'normal';
						if(obj.mode != mode) {
							obj.mode = mode;
							obj.className = mode == 'selected' ? 'hover' : '';
						}
					}
				},
				formatFontsize:function(csssize) {
					switch(csssize) {
						case '7.5pt':
						case '10px': return 1;
						case '13px':
						case '10pt': return 2;
						case '16px':
						case '12pt': return 3;
						case '18px':
						case '14pt': return 4;
						case '24px':
						case '18pt': return 5;
						case '32px':
						case '24pt': return 6;
						case '48px':
						case '36pt': return 7;
						default: return '大小';
					}
				},
				initEditor:function() {
					var self = this, cfg = self.config, editorid=cfg.editorid;
					if(BROWSER.other) {
						PZEM(cfg.editorid + '_controls').style.display = 'none';
						return;
					}
					var buttons = PZEM(cfg.editorid + '_controls').getElementsByTagName('a');
					for(var i = 0; i < buttons.length; i++) {
						if(buttons[i].id.indexOf(editorid + '_') != -1) {
							buttons[i].href = 'javascript:;';
							_attachEvent(buttons[i], 'mouseover', function(e) {self.setEditorTip(BROWSER.ie ? window.event.srcElement.title : e.target.title);});
							if(buttons[i].id.substr(buttons[i].id.indexOf('_') + 1) == 'url') {
								buttons[i].onclick = function(e) {self.puzzcode('unlink');self.puzzcode('url');doane();};
							} else {
								if(!buttons[i].getAttribute('init')) {
									buttons[i].onclick = function(e) {self.puzzcode(this.id.substr(this.id.indexOf('_') + 1));doane();};
								}
							}
							buttons[i].onmouseout = function(e) {self.setEditorTip('');};
						}
					}
					var editor_controls = PZEM(editorid + '_controls');
					self.setUnselectable(editor_controls);
					if(cfg.needfocus){
						self.checkFocus();
					}
					self.checklength();
				},
				getEditorContents:function() {
					var self = this, cfg = self.config, editorid=cfg.editorid;
					return self.wysiwyg ? self.editdoc.body.innerHTML :self.editdoc.value;
				},
				checkFocus:function() {
					var self = this, cfg = self.config, editorid=cfg.editorid;
					if(self.wysiwyg) {
						try {
							self.editwin.focus();
						} catch(e) {
							self.editwin.document.body.focus();
						}
					} else {
						self.textobj.focus();
					}
				},
				setUnselectable:function(obj) {
					var self = this, cfg = self.config, editorid=cfg.editorid;
					if(BROWSER.ie && BROWSER.ie > 4 && typeof obj.tagName != 'undefined') {
						if(obj.hasChildNodes()) {
							for(var i = 0; i < obj.childNodes.length; i++) {
								self.setUnselectable(obj.childNodes[i]);
							}
						}
						if(obj.tagName != 'INPUT') {
							obj.unselectable = 'on';
						}
					}
				},
				setEditorTip:function(s) {
					var self = this, cfg = self.config, editorid=cfg.editorid;
					PZEM(editorid + '_tip').innerHTML = '&nbsp;' + s;
				},
				insertAt:function(txt) {
					var self = this, cfg = self.config, editorid=cfg.editorid;
					self.checkFocus();
					var code = '@';
					if(txt) code += txt+' ';
					self.insertText(code, strlen(code), 0);
					hideMenu();
				},
				puzzcode:function(cmd, arg) {
					var self = this, cfg = self.config, editdoc=self.editdoc,editbox=self.editbox, editorid=cfg.editorid,wysiwyg = self.wysiwyg;
					if(cmd != 'redo') {
						self.addSnapshot(self.getEditorContents());
					}
					self.checkFocus();
					if(in_array(cmd, ['attach','sml', 'url', 'quote', 'code', 'free', 'hide', 'aud', 'vid', 'fls', 'image', 'pasteword']) || cmd == 'tbl' || in_array(cmd, ['fontname', 'fontsize', 'forecolor', 'backcolor']) && !arg) {
						self.showEditorMenu(cmd);
						return;
					} else if(cmd == 'autotypeset') {
						self.autoTypeset();
						return;
					} else if(cmd == 'at') {
						self.insertAt();
						return;
					} else if(!wysiwyg && cmd == 'removeformat') {
						var simplestrip = new Array('b', 'i', 'u');
						var complexstrip = new Array('font', 'color', 'backcolor', 'size');

						var str = self.getSel();
						if(str === false) {
							return;
						}
						for(var tag in simplestrip) {
							str = self.stripSimple(simplestrip[tag], str);
						}
						for(var tag in complexstrip) {
							str = self.stripComplex(complexstrip[tag], str);
						}
						self.insertText(str);
					} else if(cmd == 'undo') {
						self.addSnapshot(self.getEditorContents());
						self.moveCursor(-1);
						if((str = self.getSnapshot()) !== false) {
							if(wysiwyg) {
								editdoc.body.innerHTML = str;
							} else {
								editdoc.value = str;
							}
						}
					} else if(cmd == 'redo') {
						self.moveCursor(1);
						if((str = self.getSnapshot()) !== false) {
							if(wysiwyg) {
								editdoc.body.innerHTML = str;
							} else {
								editdoc.value = str;
							}
						}
					}else if(!wysiwyg && cmd == 'unlink') {
						var sel = self.getSel();
						sel = self.stripSimple('url', sel);
						sel = self.stripComplex('url', sel);
						self.insertText(sel);
					} else if(cmd == 'floatleft' || cmd == 'floatright') {
						var arg = cmd == 'floatleft' ? 'left' : 'right';
						if(wysiwyg) {
							if(txt = self.getSel()) {
								argm = arg == 'left' ? 'right' : 'left';
								self.insertText('<br style="clear: both"><table class="float" style="float: ' + arg + '; margin-' + argm + ': 5px;"><tbody><tr><td>' + txt + '</td></tr></tbody></table>', true);
							}
						} else {
							var opentag = '[float=' + arg + ']';
							var closetag = '[/float]';
							if(txt = self.getSel()) {
								txt = opentag + txt + closetag;
								self.insertText(txt, strlen(txt), 0);
							} else {
								self.insertText(opentag + closetag, opentag.length, closetag.length);
							}
						}
					} else if(cmd == 'chck') {
						self.checklength();
					} else if(cmd == 'tpr') {
						if(confirm('您确认要清除所有内容吗？')) {
							self.clearContent();
						}
					}else {
						var formatcmd = cmd == 'backcolor' && !BROWSER.ie ? 'hilitecolor' : cmd;
						try {
							var ret = self.applyFormat(formatcmd, false, (isUndefined(arg) ? true : arg));
						} catch(e) {
							var ret = false;
						}
					}

					if(cmd != 'undo') {
						self.addSnapshot(self.getEditorContents());
					}
					if(wysiwyg) {
						self.setContext(cmd);
					}
					if(in_array(cmd, ['bold', 'italic', 'underline', 'strikethrough', 'inserthorizontalrule', 'fontname', 'fontsize', 'forecolor', 'backcolor', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', 'floatleft', 'floatright', 'removeformat', 'unlink', 'undo', 'redo'])) {
						hideMenu();
					}
					doane();
					return ret;
				},
				wrapTags: function (tagname, useoption, selection) {
					var self = this, cfg = self.config;
					if(isUndefined(selection)) {
						var selection = self.getSel();
						if(selection === false) {
							selection = '';
						} else {
							selection += '';
						}
					}

					if(useoption !== false) {
						var opentag = '[' + tagname + '=' + useoption + ']';
					} else {
						var opentag = '[' + tagname + ']';
					}

					var closetag = '[/' + tagname + ']';
					var text = opentag + selection + closetag;
					self.insertText(text, strlen(opentag), strlen(closetag), in_array(tagname, ['code', 'quote', 'free', 'hide']) ? true : false);
				},
				applyFormat:function(cmd, dialog, argument) {
					var self = this, editdoc=self.editdoc, wysiwyg = self.wysiwyg;
					if(wysiwyg) {
						editdoc.execCommand(cmd, (isUndefined(dialog) ? false : dialog), (isUndefined(argument) ? true : argument));
						return;
					}
					switch(cmd) {
						case 'bold':
						case 'italic':
						case 'underline':
						case 'strikethrough':
							self.wrapTags(cmd.substr(0, 1), false);
							break;
						case 'inserthorizontalrule':
							self.insertText('[hr]', 4, 0);
							break;
						case 'justifyleft':
						case 'justifycenter':
						case 'justifyright':
							self.wrapTags('align', cmd.substr(7));
							break;
						case 'fontname':
							self.wrapTags('font', argument);
							break;
						case 'fontsize':
							self.wrapTags('size', argument);
							break;
						case 'forecolor':
							self.wrapTags('color', argument);
							break;
						case 'hilitecolor':
						case 'backcolor':
							self.wrapTags('backcolor', argument);
							break;
					}
				},
				clearContent:function() {
					var self = this, cfg = self.config;
					if(self.wysiwyg) {
						self.editdoc.body.innerHTML = BROWSER.firefox ? '<br />' : '';
					} else {
						self.textobj.value = '';
					}
				},
				showEditorMenu:function(tag, params) {
					var self = this, cfg = self.config, editdoc=self.editdoc,editbox=self.editbox, editorid=cfg.editorid,wysiwyg = self.wysiwyg;
					var sel, selection;
					var str = '', strdialog = 0, stitle = '';
					var ctrlid = editorid + (params ? '_cst' + params + '_' : '_') + tag;
					var opentag = '[' + tag + ']';
					var closetag = '[/' + tag + ']';
					var menu = PZEM(ctrlid + '_menu');
					var pos = [0, 0];
					var menuwidth = 270;
					var menupos = '43!';
					var menutype = 'menu';

					if(BROWSER.ie) {
						sel = self.wysiwyg ? self.editdoc.selection.createRange() : document.selection.createRange();
						pos = self.getCaret();
					}

					selection = sel ? (self.wysiwyg ? sel.htmlText : sel.text) : self.getSel();

					if(menu) {
						if(PZEM(ctrlid).getAttribute('menupos') !== null) {
							menupos = PZEM(ctrlid).getAttribute('menupos');
						}
						if(PZEM(ctrlid).getAttribute('menuwidth') !== null) {
							menu.style.width = PZEM(ctrlid).getAttribute('menuwidth') + 'px';
						}
						if(menupos == '00') {
							menu.className = 'fwinmask';
							if(PZEM(editorid + '_' + tag + '_menu').style.visibility == 'hidden') {
								PZEM(editorid + '_' + tag + '_menu').style.visibility = 'visible';
							} else {
								showMenu({'ctrlid':ctrlid,'mtype':'win','evt':'click','pos':menupos,'timeout':250,'duration':3,'drag':ctrlid + '_ctrl'});
							}
						} else {
							if(tag=='attach'){
								showMenu({'ctrlid':ctrlid,'evt':'click','duration':3,'cache':1,'drag':ctrlid+'_drag','pos':menupos});
							}else{
								showMenu({'ctrlid':ctrlid,'evt':'click','pos':menupos,'timeout':250,'duration':in_array(tag, ['fontname', 'fontsize', 'sml']) ? 2 : 3,'drag':1});
							}
							
						}

					} else {
						switch(tag) {
							case 'url':
								str = '请输入链接地址:<br /><input type="text" id="' + ctrlid + '_param_1" style="width: 95%" value="" class="px" />'+
									(selection ? '' : '<br />请输入链接文字:<br /><input type="text" id="' + ctrlid + '_param_2" style="width: 95%" value="" class="px" />');
								break;
							case 'forecolor':
								self.showColorBox(ctrlid, 1);
								return;
							case 'backcolor':
								self.showColorBox(ctrlid, 1, '', 1);
								return;
							case 'sml':
								self.showSmiles(ctrlid, 1);
								return;
							case 'attach':
								self.showAttach(ctrlid, 1);
								return;
							case 'fontname':
								self.showFontList(ctrlid, 1);
								return;
							case 'fontsize':
								self.showFontSizeList(ctrlid, 1);
								return;
							case 'code':
								if(wysiwyg) {
									opentag = '<div class="blockcode"><blockquote>';
									closetag = '</blockquote></div><br />';
								}
							case 'quote':
								if(wysiwyg && tag == 'quote') {
									opentag = '<div class="quote"><blockquote>';
									closetag = '</blockquote></div><br />';
								}
							case 'tbl':
								str = '<p class="pbn">表格行数: <input type="text" id="' + ctrlid + '_param_1" size="2" value="2" class="px" /> &nbsp; 表格列数: <input type="text" id="' + ctrlid + '_param_2" size="2" value="2" class="px" /></p><p class="pbn">表格宽度: <input type="text" id="' + ctrlid + '_param_3" size="2" value="" class="px" /> &nbsp; 背景颜色: <input type="text" id="' + ctrlid + '_param_4" size="2" class="px" onclick="showColorBox(this.id, 2)" /></p><p class="xg2 pbn" style="cursor:pointer" onclick="showDialog(PZEM(\'tbltips_msg\').innerHTML, \'notice\', \'小提示\', null, 0)"><img id="tbltips" title="小提示" class="vm" src="' + IMGDIR + '/info_small.gif"> 快速书写表格提示</p>';
								str += '<div id="tbltips_msg" style="display: none">“[tr=颜色]” 定义行背景<br />“[td=宽度]” 定义列宽<br />“[td=列跨度,行跨度,宽度]” 定义行列跨度<br /><br />快速书写表格范例：<div class=\'xs0\' style=\'margin:0 5px\'>[table]<br />Name:|Discuz!<br />Version:|X1<br />[/table]</div>用“|”分隔每一列，表格中如有“|”用“\\|”代替，换行用“\\n”代替。</div>';
								break;
							default:
								break;
						}

						var menu = document.createElement('div');
						menu.id = ctrlid + '_menu';
						menu.style.display = 'none';
						menu.className = 'p_pof upf';
						menu.style.width = menuwidth + 'px';
						if(menupos == '00') {
							menu.className = 'fwinmask';
							s = '<table width="100%" cellpadding="0" cellspacing="0" class="fwin"><tr><td class="t_l"></td><td class="t_c"></td><td class="t_r"></td></tr><tr><td class="m_l">&nbsp;&nbsp;</td><td class="m_c">'
								+ '<h3 class="flb"><em>' + stitle + '</em><span><a onclick="javascript:$.oFEditor(\''+cfg.id+'\').hideMenu(\'\', \'win\');return false;" class="flbc" href="javascript:;">X</a></span></h3><div class="c">' + str + '</div>'
								+ '<p class="o pns"><button type="submit" id="' + ctrlid + '_submit" class="btn btn-success">提交</button></p>'
								+ '</td><td class="m_r"></td></tr><tr><td class="b_l"></td><td class="b_c"></td><td class="b_r"></td></tr></table>';
						} else {
							s = '<div class="p_opt cl"><span class="y" style="margin:-10px -10px 0 0"><a onclick="javascript:$.oFEditor(\''+cfg.id+'\').hideMenu();return false;" class="flbc" href="javascript:;">X</a></span><div>' + str + '</div><div class="pns mtn"><button type="submit" id="' + ctrlid + '_submit" class="btn btn-success pull-right">提交</button></div></div>';
						}
						menu.innerHTML = s;
						$('body').append(menu);
						showMenu({'ctrlid':ctrlid,'mtype':menutype,'evt':'click','duration':3,'cache':0,'drag':1,'pos':menupos});
					}

					try {
						if(PZEM(ctrlid + '_param_1')) {
							PZEM(ctrlid + '_param_1').focus();
						}
					} catch(e) {}
					var objs = menu.getElementsByTagName('*');
					for(var i = 0; i < objs.length; i++) {
						_attachEvent(objs[i], 'keydown', function(e) {
							e = e ? e : event;
							obj = BROWSER.ie ? event.srcElement : e.target;
							if((obj.type == 'text' && e.keyCode == 13) || (obj.type == 'textarea' && e.ctrlKey && e.keyCode == 13)) {
								if(PZEM(ctrlid + '_submit') && tag != 'image') PZEM(ctrlid + '_submit').click();
								doane(e);
							} else if(e.keyCode == 27) {
								hideMenu();
								doane(e);
							}
						});
					}
					if(PZEM(ctrlid + '_submit')) PZEM(ctrlid + '_submit').onclick = function() {
						self.checkFocus();
						if(BROWSER.ie && wysiwyg) {
							self.setCaret(pos[0]);
						}
						switch(tag) {
							case 'url':
								var href = PZEM(ctrlid + '_param_1').value;
								href = (isEmail(href) ? 'mailto:' : '') + href;
								if(href != '') {
									var v = selection ? selection : (PZEM(ctrlid + '_param_2').value ? PZEM(ctrlid + '_param_2').value : href);
									str = wysiwyg ? ('<a href="' + href + '">' + v + '</a>') : '[url=' + squarestrip(href) + ']' + v + '[/url]';
									if(wysiwyg) self.insertText(str, str.length - v.length, 0, (selection ? true : false), sel);
									else self.insertText(str, str.length - v.length - 6, 6, (selection ? true : false), sel);
								}
								break;
							case 'code':
								if(wysiwyg) {
									opentag = '<div class="blockcode"><blockquote>';
									closetag = '</blockquote></div><br />';
									if(!BROWSER.ie) {
										selection = selection ? selection : '\n';
									}
								}
							case 'quote':
								if(wysiwyg && tag == 'quote') {
									opentag = '<div class="quote"><blockquote>';
									closetag = '</blockquote></div><br />';
									if(!BROWSER.ie) {
										selection = selection ? selection : '\n';
									}
								}
							case 'tbl':
								var rows = PZEM(ctrlid + '_param_1').value;
								var columns = PZEM(ctrlid + '_param_2').value;
								var width = PZEM(ctrlid + '_param_3').value;
								var bgcolor = PZEM(ctrlid + '_param_4').value;
								rows = /^[-\+]?\d+$/.test(rows) && rows > 0 && rows <= 30 ? rows : 2;
								columns = /^[-\+]?\d+$/.test(columns) && columns > 0 && columns <= 30 ? columns : 2;
								width = width.substr(width.length - 1, width.length) == '%' ? (width.substr(0, width.length - 1) <= 98 ? width : '98%') : (width <= 560 ? width + 'px' : '98%');
								bgcolor = /[\(\)%,#\w]+/.test(bgcolor) ? bgcolor : '';
								if(wysiwyg) {
									str = '<table cellspacing="0" cellpadding="0" style="width:' + (width ? width : '50%') + '" class="t_table"' + (bgcolor ? ' bgcolor="' + bgcolor + '"' : '') + '>';
									for (var row = 0; row < rows; row++) {
										str += '<tr>\n';
										for (col = 0; col < columns; col++) {
											str += '<td>&nbsp;</td>\n';
										}
										str += '</tr>\n';
									}
									str += '</table>\n';
								} else {
									str = '[table=' + (width ? width : '50%') + (bgcolor ? ',' + bgcolor : '') + ']\n';
									for (var row = 0; row < rows; row++) {
										str += '[tr]';
										for (col = 0; col < columns; col++) {
											str += '[td] [/td]';
										}
										str += '[/tr]\n';
									}
									str += '[/table]\n';
								}
								self.insertText(str, str.length, 0, false, sel);
								break;
						
							default:
								if(!str) {
									str = '';
									var first = PZEM(ctrlid + '_param_1').value;
									if(PZEM(ctrlid + '_param_2')) var second = PZEM(ctrlid + '_param_2').value;
									if(PZEM(ctrlid + '_param_3')) var third = PZEM(ctrlid + '_param_3').value;
									if((params == 1 && first) || (params == 2 && first && (haveSel || second)) || (params == 3 && first && second && (haveSel || third))) {
										if(params == 1) {
											str = first;
										} else if(params == 2) {
											str = haveSel ? selection : second;
											opentag = '[' + tag + '=' + first + ']';
										} else {
											str = haveSel ? selection : third;
											opentag = '[' + tag + '=' + first + ',' + second + ']';
										}
										self.insertText((opentag + str + closetag), strlen(opentag), strlen(closetag), true, sel);
									}
								}
								break;
						}
						hideMenu();
					};
				},
				switchEditor:function(mode) {
					var self = this, cfg = self.config, editorid=cfg.editorid,wysiwyg=self.wysiwyg;
					if(mode == wysiwyg)  {
						return;
					}
					if(!mode) {
						var controlbar = PZEM(editorid + '_controls');
						var controls = [];
						var buttons = controlbar.getElementsByTagName('a');
						var buttonslength = buttons.length;
						for(var i = 0; i < buttonslength; i++) {
							if(buttons[i].id) {
								controls[controls.length] = buttons[i].id;
							}
						}
						var controlslength = controls.length;
						for(var i = 0; i < controlslength; i++) {
							var control = PZEM(controls[i]);

							if(control.id.indexOf(editorid + '_') != -1) {
								control.state = false;
								control.mode = 'normal';
							} else if(control.id.indexOf(editorid + '_popup_') != -1) {
								control.state = false;
							}
						}
						self.setContext('clear');
					}
					self.cursor = -1;
					self.stack = [];
					var parsedtext = self.getEditorContents();
					parsedtext = mode ? bbcode2html(parsedtext,cfg.id) : html2bbcode(parsedtext);
					self.wysiwyg = mode;
					self.textobj.setAttribute('data-wysiwyg',mode);
					
					cfg.initialtext=parsedtext;
					self._createHTML();
					self.setEditorStyle();
					self.editwin.focus();
					self.setCaretAtEnd();
				},
				hideMenu:function(attr, mtype){
					hideMenu(attr, mtype);
				},
				getSubmitValue:function(){
					var self = this,wysiwyg=self.wysiwyg;
					var parsedtext = self.getEditorContents();
					parsedtext = wysiwyg ? html2bbcode(parsedtext) : self.textobj.value;
					return parsedtext;
				},
				autoTypeset:function() {
					var self = this, cfg = self.config, editorid=cfg.editorid;
					var sel;
					if(BROWSER.ie) {
						sel = self.wysiwyg ? self.editdoc.selection.createRange() : document.selection.createRange();
					}
					var selection = sel ? (self.wysiwyg ? sel.htmlText.replace(/<\/?p>/ig, '<br />') : sel.text) : self.getSel();
					selection = trim(selection);
					selection = self.wysiwyg ? selection.replace(/<br( \/)?>(<br( \/)?>)+/ig, '</p>\n<p style="line-height: 30px; text-indent: 2em;">') : selection.replace(/\n\n+/g, '[/p]\n[p=30, 2, left]');
					opentag = self.wysiwyg ? '<p style="line-height: 30px; text-indent: 2em;">' : '[p=30, 2, left]';
					var s = opentag + selection + (self.wysiwyg ? '</p>' : '[/p]');
					self.insertText(s, strlen(opentag), 4, false, sel);
					self.hideMenu();
				},
				getSel:function() {
					var self = this, cfg = self.config, editorid=cfg.editorid;
					if(self.wysiwyg) {
						try {
							selection = self.editwin.getSelection();
							self.checkFocus();
							range = selection ? selection.getRangeAt(0) : self.editdoc.createRange();
							return self.readNodes(range.cloneContents(), false);
						} catch(e) {
							try {
								var range = self.editdoc.selection.createRange();
								if(range.htmlText && range.text) {
									return range.htmlText;
								} else {
									var htmltext = '';
									for(var i = 0; i < range.length; i++) {
										htmltext += range.item(i).outerHTML;
									}
									return htmltext;
								}
							} catch(e) {
								return '';
							}
						}
					} else {
						if(!isUndefined(self.editdoc.selectionStart)) {
							return self.editdoc.value.substr(self.editdoc.selectionStart, self.editdoc.selectionEnd - self.editdoc.selectionStart);
						} else if(document.selection && document.selection.createRange) {
							return document.selection.createRange().text;
						} else if(window.getSelection) {
							return window.getSelection() + '';
						} else {
							return false;
						}
					}
				},
				insertText:function(text, movestart, moveend, select, sel) {
					var self = this, cfg = self.config, editdoc=self.editdoc,editbox=self.editbox, editorid=cfg.editorid;
					self.checkFocus();
					if(self.wysiwyg) {
						try {
							self.editdoc.execCommand('insertHTML', false, text);
						} catch(e) {
							if(!isUndefined(self.editdoc.selection) && self.editdoc.selection.type != 'Text' && self.editdoc.selection.type != 'None') {
								movestart = false;
								self.editdoc.selection.clear();
							}

							if(isUndefined(sel)) {
								sel = self.editdoc.selection.createRange();
							}

							sel.pasteHTML(text);

							if(text.indexOf('\n') == -1) {
								if(!isUndefined(movestart)) {
									sel.moveStart('character', -strlen(text) + movestart);
									sel.moveEnd('character', -moveend);
								} else if(movestart != false) {
									sel.moveStart('character', -strlen(text));
								}
								if(!isUndefined(select) && select) {
									sel.select();
								}
							}
						}
					} else {
						if(!isUndefined(editdoc.selectionStart)) {
							if(editdoc._selectionStart) {
								editdoc.selectionStart = editdoc._selectionStart;
								editdoc.selectionEnd = editdoc._selectionEnd;
								editdoc._selectionStart = 0;
								editdoc._selectionEnd = 0;
							}
							var opn = editdoc.selectionStart + 0;
							editdoc.value = editdoc.value.substr(0, editdoc.selectionStart) + text + editdoc.value.substr(editdoc.selectionEnd);

							if(!isUndefined(movestart)) {
								editdoc.selectionStart = opn + movestart;
								editdoc.selectionEnd = opn + strlen(text) - moveend;
							} else if(movestart !== false) {
								editdoc.selectionStart = opn;
								editdoc.selectionEnd = opn + strlen(text);
							}
						} else if(document.selection && document.selection.createRange) {
							if(isUndefined(sel)) {
								sel = document.selection.createRange();
							}
							if(editbox.sel) {
								sel = editbox.sel;
								editbox.sel = null;
							}
							sel.text = text.replace(/\r?\n/g, '\r\n');
							if(!isUndefined(movestart)) {
								sel.moveStart('character', -strlen(text) +movestart);
								sel.moveEnd('character', -moveend);
							} else if(movestart !== false) {
								sel.moveStart('character', -strlen(text));
							}
							sel.select();
						} else {
							editdoc.value += text;
						}
					}
					self.checkFocus();
				},
				stripSimple:function(tag, str, iterations) {
					var opentag = '[' + tag + ']';
					var closetag = '[/' + tag + ']';

					if(isUndefined(iterations)) {
						iterations = -1;
					}
					while((startindex = stripos(str, opentag)) !== false && iterations != 0) {
						iterations --;
						if((stopindex = stripos(str, closetag)) !== false) {
							var text = str.substr(startindex + opentag.length, stopindex - startindex - opentag.length);
							str = str.substr(0, startindex) + text + str.substr(stopindex + closetag.length);
						} else {
							break;
						}
					}
					return str;
				},
				stripComplex:function(tag, str, iterations) {
					var opentag = '[' + tag + '=';
					var closetag = '[/' + tag + ']';

					if(isUndefined(iterations)) {
						iterations = -1;
					}
					while((startindex = stripos(str, opentag)) !== false && iterations != 0) {
						iterations --;
						if((stopindex = stripos(str, closetag)) !== false) {
							var openend = stripos(str, ']', startindex);
							if(openend !== false && openend > startindex && openend < stopindex) {
								var text = str.substr(openend + 1, stopindex - openend - 1);
								str = str.substr(0, startindex) + text + str.substr(stopindex + closetag.length);
							} else {
								break;
							}
						} else {
							break;
						}
					}
					return str;
				},
				stripos:function(haystack, needle, offset) {
					if(isUndefined(offset)) {
						offset = 0;
					}
					var index = haystack.toLowerCase().indexOf(needle.toLowerCase(), offset);

					return (index == -1 ? false : index);
				},
				readNodes:function(root, toptag) {
					var html = "";
					var moz_check = /_moz/i;

					switch(root.nodeType) {
						case Node.ELEMENT_NODE:
						case Node.DOCUMENT_FRAGMENT_NODE:
							var closed;
							if(toptag) {
								closed = !root.hasChildNodes();
								html = '<' + root.tagName.toLowerCase();
								var attr = root.attributes;
								for(var i = 0; i < attr.length; ++i) {
									var a = attr.item(i);
									if(!a.specified || a.name.match(moz_check) || a.value.match(moz_check)) {
										continue;
									}
									html += " " + a.name.toLowerCase() + '="' + a.value + '"';
								}
								html += closed ? " />" : ">";
							}
							for(var i = root.firstChild; i; i = i.nextSibling) {
								html += readNodes(i, true);
							}
							if(toptag && !closed) {
								html += "</" + root.tagName.toLowerCase() + ">";
							}
							break;

						case Node.TEXT_NODE:
							html = htmlspecialchars(root.data);
							break;
					}
					return html;
				},
				getCaret:function() {
					var self = this, cfg = self.config;
					if(self.wysiwyg) {
						var obj = self.editdoc.body;
						var s = document.selection.createRange();
						s.setEndPoint('StartToStart', obj.createTextRange());
						var matches1 = s.htmlText.match(/<\/p>/ig);
						var matches2 = s.htmlText.match(/<br[^\>]*>/ig);
						var fix = (matches1 ? matches1.length - 1 : 0) + (matches2 ? matches2.length : 0);
						var pos = s.text.replace(/\r?\n/g, ' ').length;
						if(matches3 = s.htmlText.match(/<img[^\>]*>/ig)) pos += matches3.length;
						if(matches4 = s.htmlText.match(/<\/tr|table>/ig)) pos += matches4.length;
						return [pos, fix];
					} else {
						self.checkFocus();
						var sel = document.selection.createRange();
						self.editbox.sel = sel;
						self.editdoc._selectionStart = self.editdoc.selectionStart;
						self.editdoc._selectionEnd = self.editdoc.selectionEnd;
					}
				},
				setCaret:function(pos) {
					var self = this, cfg = self.config;
					var obj = self.wysiwyg ? self.editdoc.body : self.editbox;
					var r = obj.createTextRange();
					r.moveStart('character', pos);
					r.collapse(true);
					r.select();
				},
				
				showColorBox:function(ctrlid, layer, k, bgcolor) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					var tag1 = !bgcolor ? 'color' : 'backcolor', tag2 = !bgcolor ? 'forecolor' : 'backcolor';
					self.checkFocus();
					if(!PZEM(ctrlid + '_menu')) {
						var menu = document.createElement('div');
						menu.id = ctrlid + '_menu';
						menu.className = 'p_pop colorbox';
						menu.unselectable = true;
						menu.style.display = 'none';
						var coloroptions = ['Black', 'Sienna', 'DarkOliveGreen', 'DarkGreen', 'DarkSlateBlue', 'Navy', 'Indigo', 'DarkSlateGray', 'DarkRed', 'DarkOrange', 'Olive', 'Green', 'Teal', 'Blue', 'SlateGray', 'DimGray', 'Red', 'SandyBrown', 'YellowGreen', 'SeaGreen', 'MediumTurquoise', 'RoyalBlue', 'Purple', 'Gray', 'Magenta', 'Orange', 'Yellow', 'Lime', 'Cyan', 'DeepSkyBlue', 'DarkOrchid', 'Silver', 'Pink', 'Wheat', 'LemonChiffon', 'PaleGreen', 'PaleTurquoise', 'LightBlue', 'Plum', 'White'];
						var colortexts = ['黑色', '赭色', '暗橄榄绿色', '暗绿色', '暗灰蓝色', '海军色', '靛青色', '墨绿色', '暗红色', '暗桔黄色', '橄榄色', '绿色', '水鸭色', '蓝色', '灰石色', '暗灰色', '红色', '沙褐色', '黄绿色', '海绿色', '间绿宝石', '皇家蓝', '紫色', '灰色', '红紫色', '橙色', '黄色', '酸橙色', '青色', '深天蓝色', '暗紫色', '银色', '粉色', '浅黄色', '柠檬绸色', '苍绿色', '苍宝石绿', '亮蓝色', '洋李色', '白色'];
						var str = '';
						for(var i = 0; i < 40; i++) {
							str += '<input type="button" style="background-color: ' + coloroptions[i] + '"' + ' onmouseover="javascript:$.oFEditor(\''+cfg.id+'\').setEditorTip(\'' + colortexts[i] + '\')" onmouseout="javascript:$.oFEditor(\''+cfg.id+'\').setEditorTip(\'\')"' + ' onclick="javascript:'
							+ '$.oFEditor(\''+cfg.id+'\').puzzcode(\'' + tag2 + '\', \'' + coloroptions[i] + '\');'
							+ '" title="' + colortexts[i] + '" />' + (i < 39 && (i + 1) % 8 == 0 ? '<br />' : '');
						}
						menu.innerHTML = str;
						$('body').append(menu);
					}
					showMenu({'ctrlid':ctrlid,'evt':'click','layer':layer});
				},

				showAttach:function(ctrlid, layer) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					if(!PZEM(ctrlid + '_menu')) {
						var tpl="forum_attach";
						getTpl(tpl,function(){
							var tpl_ins = $('#forum_attach_tpl');
							var pbody = $(tpl_ins.html()).find('div.pbody');
							var pwidth = $(pbody).attr('data-width');
							var ptitle = $(pbody).attr('data-title');
							var pcss = $(pbody).attr('data-css-class');
							var content = $(pbody).html();
							 
							var menupos = '43!';
							
							var menu = document.createElement('div');
							menu.id = ctrlid + '_menu';
							menu.style.display = 'none';
							menu.className = 'forum-dialog';
							menu.style.width = pwidth + 'px';
							s = '<div class="hd" id="' + ctrlid + '_drag"><button type="button" class="close" onclick="javascript:$.oFEditor(\''+cfg.id+'\').hideMenu();return false;">×</button><h3>'+ptitle+'</h3></div><div class="bd">' + content + '</div><div class="ui-footer"><button type="submit" id="' + ctrlid + '_submit" class="btn btn-primary">提交</button></div>';
							menu.innerHTML = s;
							$('body').append(menu);
							showMenu({'ctrlid':ctrlid,'mtype':'menu','evt':'click','duration':3,'cache':1,'drag':ctrlid+'_drag','pos':menupos});
							initswfu();
						 });
					}else{
						showMenu({'ctrlid':ctrlid,'evt':'click','layer':layer});
					}
				},
				
				showSmiles:function(ctrlid, layer) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					if(!PZEM(ctrlid + '_menu')) {
						ajaxget(url_smiles, {}, 
						function(result){
							var lis = Mustache.render('{{#data}}<a href="javascript:;" onclick="javascript:$.oFEditor(\''+cfg.id+'\').insertSmiley({{smile_id}});"><img id="smilie_{{smile_id}}" src="'+base_url+'assets/img/smiles/default/{{url}}" alt="{{code}}"/></a>{{/data}}', result);
							var menu = document.createElement('div');
							menu.id = ctrlid + '_menu';
							menu.className = 'p_pop smilebox';
							menu.unselectable = true;
							menu.style.display = 'none';
							menu.innerHTML = lis;
							$('body').append(menu);
							showMenu({'ctrlid':ctrlid,'evt':'click','layer':layer});
						},
						function(result){
							showError(result.message);
						});
					}else{
						showMenu({'ctrlid':ctrlid,'evt':'click','layer':layer});
					}
				},
				insertSmiley:function (smilieid) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					self.checkFocus();
					var src = PZEM('smilie_' + smilieid).src;
					var code = PZEM('smilie_' + smilieid).alt;
					if(wysiwyg) {
						self.insertText('<img src="' + src + '" border="0" smilieid="' + smilieid + '" alt="" />', false);
					} else {
						code += ' ';
						self.insertText(code, strlen(code), 0);
					}
					hideMenu();
				},
				insertAttachTag:function (aid) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					self.checkFocus();
					var txt = '[attach]' + aid + '[/attach]';
					if(wysiwyg) {
						self.insertText(txt, false);
					} else {
						self.insertText(txt, strlen(txt), 0);
					}
				},
				insertFlvTag:function (flv) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					self.checkFocus();
					var txt = '[flv=600,450]' + flv + '[/flv]';
					if(wysiwyg) {
						self.insertText(txt, false);
					} else {
						self.insertText(txt, strlen(txt), 0);
					}
				},
				insertAttachimgTag:function (aid) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					self.checkFocus();
					var src = PZEM(cfg.id+'_' + aid).src;
					
					if(wysiwyg) {
						self.insertText('<img src="' + src + '" border="0" aid="attachimg_' + aid + '" style="max-width:400px" alt="" />', false);
					} else {
						var txt = '[attachimg]' + aid + '[/attachimg]';
						self.insertText(txt, strlen(txt), 0);
					}
				},
				insertRemoteFetchImgTag:function (url) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					self.checkFocus();
					if(wysiwyg) {
						self.insertText('<img src="' + url + '" border="0" remoteimg="url" style="max-width:400px" alt="" />', false);
					} else {
						var txt = '[remoteimg]' + url + '[/remoteimg]';
						self.insertText(txt, strlen(txt), 0);
					}
				},
				showFontSizeList:function(ctrlid, layer) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					if(!PZEM(ctrlid + '_menu')) {
						var menu = document.createElement('div');
						menu.id = ctrlid + '_menu';
						menu.className = 'p_pop fszm';
						menu.unselectable = true;
						menu.style.display = 'none';
						var str = '<ul unselectable="on">';
						for(var i = 1; i < 8; i++) {
							str += '<li onclick="javascript:$.oFEditor(\''+cfg.id+'\').puzzcode(\'fontsize\', \''+i+'\')" unselectable="on"><a href="javascript:;" title="'+i+'"><font size="'+i+'" unselectable="on">'+i+'</a></li>';
						}
						str += '</ul>';
						menu.innerHTML = str;
						$('body').append(menu);
					}
					showMenu({'ctrlid':ctrlid,'evt':'click','layer':layer});
				},
				ctlent:function(event) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					var ctlent_enable = {8:1,9:1,13:1};
					if(ctlent_enable[9] && event.keyCode == 9) {
						doane(event);
					}
					if(ctlent_enable[8] && event.keyCode == 8 && wysiwyg) {
						var sel = self.getSel();
						if(sel) {
							self.insertText('', sel.length - 1, 0);
							doane(event);
						}
					}
				},
				showFontList:function(ctrlid, layer) {
					var self = this, cfg = self.config,wysiwyg=self.wysiwyg,editorid=self.editorid;
					if(!PZEM(ctrlid + '_menu')) {
						var menu = document.createElement('div');
						menu.id = ctrlid + '_menu';
						menu.className = 'p_pop fnm';
						menu.unselectable = true;
						menu.style.display = 'none';
						var str = '<ul unselectable="on">';
						var fontoptions = new Array("宋体", "黑体", "微软雅黑", "Arial", "Verdana", "simsun", "Helvetica", "Trebuchet MS", "Tahoma", "Impact", "Times New Roman", "仿宋,仿宋_GB2312", "楷体,楷体_GB2312");
						for(i in fontoptions) {
							str += '<li onclick="javascript:$.oFEditor(\''+cfg.id+'\').puzzcode(\'fontname\', \''+fontoptions[i]+'\')" style="font-family: '+fontoptions[i]+'" unselectable="on"><a href="javascript:;" title="'+fontoptions[i]+'">'+fontoptions[i]+'</a></li>';
						}
						str += '</ul>';
						menu.innerHTML = str;
						$('body').append(menu);
					}
					showMenu({'ctrlid':ctrlid,'evt':'click','layer':layer});
				},
				
				_createHTML : function() {
					var self = this, cfg = self.config;
					self.cursor = -1, self.stack = [];
					self.initialized = false;
					self.textobj = PZEM(cfg.editorid+"_textarea");
					self.wysiwyg = self.textobj.getAttribute('data-wysiwyg');
					self.postminchars = self.textobj.getAttribute('data-postminchars');
					self.postmaxchars = self.textobj.getAttribute('data-postmaxchars');
					self.allowswitcheditor = self.textobj.getAttribute('data-allowswitcheditor');
					self.allowsmilies = self.textobj.getAttribute('data-allowsmilies');
					self.allowbbcode = self.textobj.getAttribute('data-allowbbcode');
					self.allowimgcode = self.textobj.getAttribute('data-allowimgcode');

					if(self.wysiwyg==1) {
						if(PZEM(cfg.editorid + '_iframe')) {
							self.editbox = PZEM(cfg.editorid + '_iframe');
						} else {
							var iframe = document.createElement('iframe');
							iframe.frameBorder = '0';
							iframe.tabIndex = 2;
							iframe.hideFocus = true;
							iframe.style.display = 'none';
							self.editbox = self.textobj.parentNode.appendChild(iframe);
							self.editbox.id = cfg.editorid + '_iframe';
						}

						self.editwin = self.editbox.contentWindow;
						self.editdoc = self.editwin.document;
						self.writeEditorContents((isUndefined(cfg.initialtext)||cfg.initialtext==''||cfg.initialtext==null)?bbcode2html(self.textobj.value,cfg.id):cfg.initialtext);
					} else {
						self.wysiwyg = false;
						self.editbox = self.editwin = self.editdoc = self.textobj;
						if(!isUndefined(cfg.initialtext)) {
							self.writeEditorContents(cfg.initialtext);
						}
						self.addSnapshot(self.textobj.value);
					}

					self.setEditorEvents();
					self.initEditor();
				}
			});

	function mix(r, s) {
		for ( var k in s) {
			r[k] = s[k];
		}
		return r;
	}

	function counter() {
		return _id_counter++;
	}
	
})(jQuery);